package com.cilico.single.components;

import android.content.Context;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.cilico.single.utils.Utils;
import com.cilico.single.view.FormLayout;

/**
 * 
 * @author lv.xiangan
 *
 */
public class RowPanel extends LinearLayout implements Panel {

	private boolean isInTable_;
	private int width_;
	private int height_;

	/**
	 * @param context
	 */
	public RowPanel(Context context) {
		super(context);
		setOrientation(LinearLayout.HORIZONTAL);
		setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		setGravity(Gravity.CENTER_VERTICAL);
	}


	public void addChild(Component cmp) {
		// TODO Auto-generated method stub

	}

	public Component child(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public int childrenAmount() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void loadAllChildren() {
		// TODO Auto-generated method stub

	}

	public void cleanText() {
		// TODO Auto-generated method stub

	}


	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}


	public View getCmpView() {
		return this;
	}

	public void mould() {

		int w = 0;
		int h = 0;
		int size = getChildCount();
		for (int i = 0; i < size; i++) {
			View v = getChildAt(i);
			try {
				if (v instanceof SelfLayout) {
					w += ((SelfLayout) v).getRealWidth();
					h = Math.max(h, ((SelfLayout) v).getRealHeight());
				} else {
					w += ((Component) v).getCmpWidth();
					int vheight = ((Component) v).getCmpHeight();
					try {
						LayoutParams lp = (LinearLayout.LayoutParams) v.getLayoutParams();
						if (v instanceof mButton && i < size - 1) {
							lp.rightMargin += 5;
						}
						w += lp.leftMargin + lp.rightMargin;
						vheight += lp.topMargin + lp.bottomMargin;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Utils.printException(e);
					}
					h = Math.max(h, vheight);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Utils.printException(e);
			}
		}
		ViewGroup.LayoutParams lp = getLayoutParams();
		if (w > 0) {
			lp.width = w;
		}
		if (h > 0) {
			lp.height = h;
		}
		if (size > 0 && !isInTable_) {
			lp.width = Component.MAX_ROW_WIDTH ;
			if (getChildAt(0) instanceof mViewFlipper){
				lp.width += Utils.getScaledValue(20);
			}
		}
		setLayoutParams(lp);
		width_ = lp.width;
		height_ = lp.height;
	}

	public void setContentText(String text) {
		// TODO Auto-generated method stub

	}

	public void setCssStyle(CssStyle style) {
		// TODO Auto-generated method stub

	}

	public void setCmpHeidht(int height) {
		height_ = height;
	}

	public void setCmpWidth(int width) {
		width_ = width;
	}

	public void shrinkWidth() {
		// TODO Auto-generated method stub

	}

	public void addView(View child) {
		try {
			Component c = (Component) child;
			int count=this.getChildCount();
			if(count>0)
			{
				Component temp=(Component)this.getChildAt(count-1);
				if(child instanceof Label){
					if (temp instanceof Label){
						((Label)child).offsetLength=((Label)temp).width_ + 15;
					} else if (temp instanceof Radio){
						((Label)child).offsetLength=((Radio)temp).width_ + 15;
					} else if (temp instanceof cCheckBox){
						((Label)child).offsetLength=((cCheckBox)temp).width_ + 15;
					}
				}
				if (child instanceof Select) {//兼容select控件超宽问题
					if (temp instanceof Label){
						((Select)child).offsetLength=((Label)temp).width_ + 15;
					}
				}
			}
			c.mould();
			super.addView(c.getCmpView());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Utils.printException(e);
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		boolean isAction = false;
		try {
			View parent = (View) getParent();
			isAction = parent.onTouchEvent(event);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Utils.printException(e);
		}
		return isAction;
	}

	public void removeChild(Component cmp) {
		// TODO Auto-generated method stub

	}

	public int getCmpHeight() {
		return height_;
	}

	public int getCmpWidth() {
		return width_;
	}

	public void setInTable(boolean inTable) {
		isInTable_ = inTable;
	}

	public boolean isInTable() {
		return isInTable_;
	}

	public CssStyle getCssStyle() {
		return null;
	}

	public void releaseResource(View view) {
		// TODO Auto-generated method stub
		Utils.releaseResource(view);
	}

	public void setForm(FormLayout formLayout) {
		// TODO Auto-generated method stub

	}

	public FormLayout getForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEncrypt(String encrypt) {
		// TODO Auto-generated method stub

	}

}
